package com.mywang.weitravel.net;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.bitmap.core.BitmapDisplayConfig;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import net.tsz.afinal.http.HttpHandler;
import net.tsz.afinal.http.PreferencesCookieStore;

import java.io.File;


public class Protocol {
    private static final String TAG = "ProtocolManager";
    private static Protocol mInstance;
    private FinalHttp finalHttp;
    //private FinalHttp downloader;
    private BitmapDisplayConfig listDisplayConfig;
    private Context context;
    private String url;
    public PreferencesCookieStore preferencesCookieStore;


    private Protocol(Context context) {
        this.context = context.getApplicationContext();
        initFinalHttp();
        preferencesCookieStore = new PreferencesCookieStore(context);
    }


    private String getAppVersion() {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return versionName;
            }
        } catch (Exception e) {
        }
        return versionName;
    }

    /**
     * 初始化数据请求
     */
    private void initFinalHttp() {
        finalHttp = new FinalHttp();
        finalHttp.configUserAgent(getUserAgentString());
        finalHttp.configRequestExecutionRetryCount(0);
        finalHttp.configTimeout(20000);
    }

    public String getUserAgentString() {
        return "mewuu/" + getAppVersion() + " (unknown; android " + Build.VERSION.SDK_INT + ")";
    }


    /**
     * 每次请求前检测cookie,去重,文件保存
     */
//    public void checkCookie(){
//        LogUtil.d("++++++++++++++++++    public void checkCookie(){");
//        CookieStore cs = getCookieStore();
//        List<Cookie> cookies = cs.getCookies();
//        if(cookies != null){
//            for(Cookie c : cookies){
//                cookieMap.put(c.getName(),c);
//            }
//        }
//        preferencesCookieStore.clear();
//        cs.clear();
//
//        for(Cookie c :cookieMap.values()){
//            LogUtil.d(" request cookie :::::::::::::::"+c);
//            preferencesCookieStore.addCookie(c);
//            cs.addCookie(c);
//        }
//        if(cs.getCookies().size()>0)
//            LogUtil.d("当前cookie = "+cs.getCookies().get(0));
//    }


    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static Protocol getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new Protocol(context.getApplicationContext());
        }
        return mInstance;
    }

    /**
     * 清楚单例
     */
    public void release() {
        finalHttp = null;
        mInstance = null;
    }

    /**
     * 通过key删除图片
     *
     * @param key C:\Program Files\Android\Android Studio\gradle\gradle-2.2.1\bin
     */
    public void cleanBitmap(String key) {
        if (TextUtils.isEmpty(key))
            return;
    }

    /**
     * get方式请求数据
     *
     * @param url
     * @param callBack
     */
    public void getJosn(String url, AjaxParams params, AjaxCallBack<? extends String> callBack) {
//        checkCoockie();
        if (params == null) {
            params = new AjaxParams();
        }
//        if (SettingUtil.getUid() != -1) {
//            params.put("uid", AESutil.encodeUid(SettingUtil.getUid() + ""));
//        }
        finalHttp.get(url, params, callBack);
    }

    /**
     * post方式请求数据
     *
     * @param url
     * @param params
     * @param callBack
     */
    public void postJosn(String url, AjaxParams params, AjaxCallBack<? extends String> callBack) {
        if (params == null) {
            params = new AjaxParams();
        }
//        if (SettingUtil.getUid() != -1) {
//            params.put("uid", AESutil.encodeUid(SettingUtil.getUid() + ""));
//        }
        finalHttp.post(url, params, callBack);
    }


    public FinalHttp getFinalHttp() {
        return finalHttp;
    }

    public HttpHandler<File> downLoad(String url, String target, AjaxCallBack<File> ajaxCallBack) {
        return finalHttp.download(url, target, ajaxCallBack);
    }
}